import { search,details } from '../services/exselect'
import { parse } from 'qs'

export default{

	namespace: 'exselect',
  
	state:{
		list: [],
    detailList:[],
		loading:false,
		modalVisible: false,
		pagination: {
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 条`,
      current: 1,
      total: null
    }
	},
	subscriptions: {
    setup ({ dispatch, history }) {
     history.listen(location => {
        if (location.pathname === '/exchange/exselect') {
          if(location.query.keyname==undefined){
               dispatch({
                type: 'query',
                payload: location.query
              })
          }else{
             dispatch({
              type: 'search',
              payload: location.query
            })
          }         
        }        
      })
    }
  },

  effects: {
    *query ({ payload }, { call, put }) {
        yield put({ type: 'showLoading' });
        let params=parse(payload)
        
        if(params.page==undefined){
          params.page=1
        }
        if(params.pageSize==undefined){
          params.pageSize=10
        }
        const data = yield call(search, params);  
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.list,
            pagination:{
              total: data.totalRows,
              current: parseInt(params.page)
            }
          }
        })
    },
     *search ({ payload }, { call, put }) {
        yield put({ type: 'showLoading' });
        let params=parse(payload)
        
        if(params.page==undefined){
          params.page=1
        }
        if(params.pageSize==undefined){
          params.pageSize=10
        }
        const data = yield call(search, params);  
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.list,
            pagination:{
              total: data.totalRows,
              current: parseInt(params.page)
            }
          }
        })
        console.log(data.totalRows)
        console.log(params.page)
    },
    *details ({ payload }, { call, put }) {

        let params=parse(payload)
        if(params.page==undefined){
          params.page=1
        }
        if(params.pageSize==undefined){
          params.pageSize=10
        }

      const data = yield call(details, payload);	   
	      yield put({
	        type: 'showModal',
	        payload: {
	        detailList: data.list
        }
      })
    }
},    
   
  reducers: {
    showLoading (state) {
      return { ...state, loading: true }
    },
    hideLoading (state) {
      return { ...state, loading: false }
    },
    showModal (state, action) {
      return { ...state, ...action.payload, modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false }
    },
    querySuccess (state, action) {
      const {list, pagination} = action.payload;
      return { ...state,
        list,
        loading: false,
        pagination: {
          ...state.pagination,
          ...pagination
        }}
    },
    clear(state, action) {
      let dataSource = {
        list: [],
        amount: 0,
        totalPrice: 0
      }
      return {
        dataSource,
        loading: false,
        pagination: {
          showSizeChanger: false,
          showQuickJumper: false,
          showTotal: total => `共 ${total} 条`,
          current: 1,
          total: null
        }
      }
    }
  }
}